<?php

/**
 * campana actions.
 *
 * @package    plagas
 * @subpackage campana
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class campanaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->campanas = Doctrine_Core::getTable('campana')->getCampanas();
//      ->createQuery('a')
//      ->execute();

    $this->filtro = new campanaFormFilter();
  }

    public function executeFilter(sfWebRequest $request)
    {
        $this->setTemplate('index');
        $this->filtro = new campanaFormFilter();
        if($request->isMethod('post'))
        {
            $this->filtro->bind($request->getParameter($this->filtro->getName()));
        }
        $query = $this->filtro->buildQuery($this->filtro->getValues());
        $id = $this->filtro->getValue('lote_id');
        if ($id == '')
        {
            $this->campanas = Doctrine_Core::getTable('campana')->getCampanas();
        }
        else
        {
            $this->campanas = $query->execute();
        }
        $this->filtro = new campanaFormFilter();
    }

  public function executeShow(sfWebRequest $request)
  {
    $this->campana = Doctrine_Core::getTable('campana')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->campana);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new campanaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new campanaForm();

    $this->processForm($request, $this->form);
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($campana = Doctrine_Core::getTable('campana')->find(array($request->getParameter('id'))), sprintf('Object campana does not exist (%s).', $request->getParameter('id')));
    $this->form = new campanaForm($campana);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($campana = Doctrine_Core::getTable('campana')->find(array($request->getParameter('id'))), sprintf('Object campana does not exist (%s).', $request->getParameter('id')));
    $this->form = new campanaForm($campana);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($campana = Doctrine_Core::getTable('campana')->find(array($request->getParameter('id'))), sprintf('Object campana does not exist (%s).', $request->getParameter('id')));

    if($campana->getMonitoreoes()->count() == 0)
    {
        $campana->delete();
        $this->getUser()->setFlash('notice', sprintf('Campaña eliminada exitosamente.'));
    }
    else
    {
        $this->getUser()->setFlash('error', sprintf('No se puede eliminar la campaña.'));
    }

    $this->redirect('campana/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $campana = $form->save();

      if ($form->isNew())
      {
          $this->getUser()->setFlash('notice', sprintf('Campaña creada exitosamente.'));
      }
      else
      {
          $this->getUser()->setFlash('notice', sprintf('Campaña modificada exitosamente.'));
      }
      $this->redirect('campana/index');
    }
  }
}
